package com.huawei.secure.android.common.ssl;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.SslErrorHandler;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import o.ftz;
import o.fua;
import o.fus.a;
import o.fuv.b;
import o.fvb;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class WebViewSSLCheckThread extends Thread {
    private static final String TAG = "WebViewSSLCheckThread";
    private SSLSocketFactory R;
    private org.apache.http.conn.ssl.SSLSocketFactory S;
    private X509HostnameVerifier T;
    private SslErrorHandler U;
    private String V;
    private Callback W;
    private HostnameVerifier hostnameVerifier;
    private Context p;

    public interface Callback {
        void onCancel(Context context, String str);

        void onProceed(Context context, String str);
    }

    @Deprecated
    public WebViewSSLCheckThread(SslErrorHandler sslErrorHandler, String str, Context context) throws CertificateException, NoSuchAlgorithmException, IOException, KeyManagementException, KeyStoreException, IllegalAccessException {
        setSslErrorHandler(sslErrorHandler);
        setUrl(str);
        setContext(context);
        setSslSocketFactory(SecureSSLSocketFactory.getInstance(context));
        setHostnameVerifier(SecureSSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        try {
            setApacheSSLSocketFactory(new SecureApacheSSLSocketFactory(null, new f(context)));
        } catch (UnrecoverableKeyException e) {
            Log.e(TAG, "WebViewSSLCheckThread: UnrecoverableKeyException : " + e.getMessage());
        }
        setApacheHostnameVerifier(SecureApacheSSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
    }

    @Deprecated
    public WebViewSSLCheckThread(SslErrorHandler sslErrorHandler, String str, SSLSocketFactory sSLSocketFactory, HostnameVerifier hostnameVerifier) {
        setSslErrorHandler(sslErrorHandler);
        setUrl(str);
        setSslSocketFactory(sSLSocketFactory);
        setHostnameVerifier(hostnameVerifier);
    }

    @Deprecated
    public WebViewSSLCheckThread(SslErrorHandler sslErrorHandler, String str, org.apache.http.conn.ssl.SSLSocketFactory sSLSocketFactory, X509HostnameVerifier x509HostnameVerifier) {
        setSslErrorHandler(sslErrorHandler);
        setUrl(str);
        setApacheSSLSocketFactory(sSLSocketFactory);
        setApacheHostnameVerifier(x509HostnameVerifier);
    }

    @Deprecated
    public WebViewSSLCheckThread(SslErrorHandler sslErrorHandler, String str, org.apache.http.conn.ssl.SSLSocketFactory sSLSocketFactory, X509HostnameVerifier x509HostnameVerifier, Callback callback, Context context) {
        this.U = sslErrorHandler;
        this.V = str;
        this.S = sSLSocketFactory;
        this.T = x509HostnameVerifier;
        this.W = callback;
        this.p = context;
    }

    @Deprecated
    public static void checkServerCertificateWithOK(SslErrorHandler sslErrorHandler, String str, Context context) {
        checkServerCertificateWithOK(sslErrorHandler, str, context, null);
    }

    @Deprecated
    public static void checkServerCertificateWithOK(final SslErrorHandler sslErrorHandler, final String str, final Context context, final Callback callback) {
        if (sslErrorHandler == null || TextUtils.isEmpty(str) || context == null) {
            Log.e(TAG, "checkServerCertificateWithOK: handler or url or context is null");
            return;
        }
        b bVar = new b();
        try {
            SSLSocketFactory secureSSLSocketFactory = new SecureSSLSocketFactory(new f(context));
            secureSSLSocketFactory.setContext(context);
            bVar.ॱ(secureSSLSocketFactory, new f(context));
            bVar.ˊ(SecureSSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            bVar.ˋ().ˎ(new a().ˎ(str).ॱ()).enqueue(new fua() {
                public void onFailure(ftz o_ftz, IOException iOException) {
                    Log.e(WebViewSSLCheckThread.TAG, "onFailure , IO Exception : " + iOException.getMessage());
                    if (callback != null) {
                        callback.onCancel(context, str);
                    } else {
                        sslErrorHandler.cancel();
                    }
                }

                public void onResponse(ftz o_ftz, fvb o_fvb) throws IOException {
                    Log.e(WebViewSSLCheckThread.TAG, "onResponse . proceed");
                    if (callback != null) {
                        callback.onProceed(context, str);
                    } else {
                        sslErrorHandler.proceed();
                    }
                }
            });
        } catch (Exception e) {
            Log.e(TAG, "checkServerCertificateWithOK: exception : " + e.getMessage());
            sslErrorHandler.cancel();
        }
    }

    @Deprecated
    public void run() {
        HttpsURLConnection httpsURLConnection;
        Exception exception;
        Throwable th;
        HttpsURLConnection httpsURLConnection2 = null;
        super.run();
        if (this.S == null || this.T == null) {
            if (this.R == null || this.hostnameVerifier == null) {
                b();
                return;
            }
            try {
                URLConnection openConnection = new URL(this.V).openConnection();
                if (openConnection instanceof HttpsURLConnection) {
                    httpsURLConnection = (HttpsURLConnection) openConnection;
                    try {
                        httpsURLConnection.setSSLSocketFactory(this.R);
                        httpsURLConnection.setHostnameVerifier(this.hostnameVerifier);
                        httpsURLConnection.setRequestMethod(HttpContants.HTTP_METHOD_GET);
                        httpsURLConnection.setConnectTimeout(10000);
                        httpsURLConnection.setReadTimeout(20000);
                        httpsURLConnection.connect();
                    } catch (Exception e) {
                        Exception exception2 = e;
                        httpsURLConnection2 = httpsURLConnection;
                        exception = exception2;
                        try {
                            Log.e(TAG, "exception : " + exception.getMessage());
                            b();
                            if (httpsURLConnection2 != null) {
                                httpsURLConnection2.disconnect();
                            }
                        } catch (Throwable th2) {
                            th = th2;
                            if (httpsURLConnection2 != null) {
                                httpsURLConnection2.disconnect();
                            }
                            throw th;
                        }
                    } catch (Throwable th3) {
                        Throwable th4 = th3;
                        httpsURLConnection2 = httpsURLConnection;
                        th = th4;
                        if (httpsURLConnection2 != null) {
                            httpsURLConnection2.disconnect();
                        }
                        throw th;
                    }
                }
                httpsURLConnection = null;
                if (httpsURLConnection != null) {
                    httpsURLConnection.disconnect();
                }
                c();
            } catch (Exception e2) {
                exception = e2;
                Log.e(TAG, "exception : " + exception.getMessage());
                b();
                if (httpsURLConnection2 != null) {
                    httpsURLConnection2.disconnect();
                }
            }
        } else if (this.U == null || TextUtils.isEmpty(this.V)) {
            Log.e(TAG, "sslErrorHandler or url is null");
            b();
        } else {
            try {
                this.S.setHostnameVerifier(this.T);
                if (this.S instanceof SecureApacheSSLSocketFactory) {
                    ((SecureApacheSSLSocketFactory) this.S).setContext(this.p);
                }
                HttpParams basicHttpParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(basicHttpParams, 30000);
                HttpConnectionParams.setSoTimeout(basicHttpParams, 30000);
                SchemeRegistry schemeRegistry = new SchemeRegistry();
                schemeRegistry.register(new Scheme("https", this.S, 443));
                schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
                HttpClient defaultHttpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(basicHttpParams, schemeRegistry), basicHttpParams);
                HttpUriRequest httpGet = new HttpGet();
                httpGet.setURI(new URI(this.V));
                Log.i(TAG, "status code is : " + defaultHttpClient.execute(httpGet).getStatusLine().getStatusCode());
                c();
            } catch (Exception exception3) {
                Log.e(TAG, "run: exception : " + exception3.getMessage());
                b();
            } finally {
                d.a(null);
            }
        }
    }

    public String getUrl() {
        return this.V;
    }

    public void setUrl(String str) {
        this.V = str;
    }

    public SslErrorHandler getSslErrorHandler() {
        return this.U;
    }

    public void setSslErrorHandler(SslErrorHandler sslErrorHandler) {
        this.U = sslErrorHandler;
    }

    public Callback getCallback() {
        return this.W;
    }

    public void setCallback(Callback callback) {
        this.W = callback;
    }

    public Context getContext() {
        return this.p;
    }

    public void setContext(Context context) {
        this.p = context;
    }

    private void b() {
        Log.i(TAG, "callbackCancel: ");
        if (this.W != null) {
            this.W.onCancel(this.p, this.V);
        } else if (this.U != null) {
            Log.i(TAG, "callbackCancel 2: ");
            this.U.cancel();
        }
    }

    private void c() {
        Log.i(TAG, "callbackProceed: ");
        if (this.W != null) {
            this.W.onProceed(this.p, this.V);
        } else if (this.U != null) {
            this.U.proceed();
        }
    }

    public org.apache.http.conn.ssl.SSLSocketFactory getApacheSSLSocketFactory() {
        return this.S;
    }

    public void setApacheSSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory sSLSocketFactory) {
        this.S = sSLSocketFactory;
    }

    public X509HostnameVerifier getApacheHostnameVerifier() {
        return this.T;
    }

    public void setApacheHostnameVerifier(X509HostnameVerifier x509HostnameVerifier) {
        this.T = x509HostnameVerifier;
    }

    public SSLSocketFactory getSslSocketFactory() {
        return this.R;
    }

    public void setSslSocketFactory(SSLSocketFactory sSLSocketFactory) {
        this.R = sSLSocketFactory;
    }

    public HostnameVerifier getHostnameVerifier() {
        return this.hostnameVerifier;
    }

    public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
        this.hostnameVerifier = hostnameVerifier;
    }
}
